using System;

using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Hardware;


namespace eHouse
{
    class AlarmWindow : Window
    {
        private Window alarmWindow;
        private Canvas canvas = new Canvas();
        private String code = "";
        private Text textCode;

        private SPI spi;

        public AlarmWindow()
        {
            alarmWindow = new Window();

            alarmWindow.Height = SystemMetrics.ScreenHeight;
            alarmWindow.Width = SystemMetrics.ScreenWidth;

            CreateWindow();
        }

        private void CreateWindow()
        {
            Text text_0 = new Text(Resources.GetFont(Resources.FontResources.NinaB), "0");
            text_0.HorizontalAlignment = HorizontalAlignment.Center;
            text_0.VerticalAlignment = VerticalAlignment.Center;
            Text text_1 = new Text(Resources.GetFont(Resources.FontResources.NinaB), "1");
            text_1.HorizontalAlignment = HorizontalAlignment.Center;
            text_1.VerticalAlignment = VerticalAlignment.Center;
            Text text_2 = new Text(Resources.GetFont(Resources.FontResources.NinaB), "2");
            text_2.HorizontalAlignment = HorizontalAlignment.Center;
            text_2.VerticalAlignment = VerticalAlignment.Center;
            Text text_3 = new Text(Resources.GetFont(Resources.FontResources.NinaB), "3");
            text_3.HorizontalAlignment = HorizontalAlignment.Center;
            text_3.VerticalAlignment = VerticalAlignment.Center;
            Text text_4 = new Text(Resources.GetFont(Resources.FontResources.NinaB), "4");
            text_4.HorizontalAlignment = HorizontalAlignment.Center;
            text_4.VerticalAlignment = VerticalAlignment.Center;
            Text text_5 = new Text(Resources.GetFont(Resources.FontResources.NinaB), "5");
            text_5.HorizontalAlignment = HorizontalAlignment.Center;
            text_5.VerticalAlignment = VerticalAlignment.Center;
            Text text_6 = new Text(Resources.GetFont(Resources.FontResources.NinaB), "6");
            text_6.HorizontalAlignment = HorizontalAlignment.Center;
            text_6.VerticalAlignment = VerticalAlignment.Center;
            Text text_7 = new Text(Resources.GetFont(Resources.FontResources.NinaB), "7");
            text_7.HorizontalAlignment = HorizontalAlignment.Center;
            text_7.VerticalAlignment = VerticalAlignment.Center;
            Text text_8 = new Text(Resources.GetFont(Resources.FontResources.NinaB), "8");
            text_8.HorizontalAlignment = HorizontalAlignment.Center;
            text_8.VerticalAlignment = VerticalAlignment.Center;
            Text text_9 = new Text(Resources.GetFont(Resources.FontResources.NinaB), "9");
            text_9.HorizontalAlignment = HorizontalAlignment.Center;
            text_9.VerticalAlignment = VerticalAlignment.Center;
            Text text_del = new Text(Resources.GetFont(Resources.FontResources.NinaB), "Del");
            text_del.HorizontalAlignment = HorizontalAlignment.Center;
            text_del.VerticalAlignment = VerticalAlignment.Center;
            

            SimpleButton button_0 = new SimpleButton(text_0, alarmWindow.Width / 3, alarmWindow.Height / 4);
            SimpleButton button_1 = new SimpleButton(text_1, alarmWindow.Width / 3, alarmWindow.Height / 4);
            SimpleButton button_2 = new SimpleButton(text_2, alarmWindow.Width / 3, alarmWindow.Height / 4);
            SimpleButton button_3 = new SimpleButton(text_3, alarmWindow.Width / 3, alarmWindow.Height / 4);
            SimpleButton button_4 = new SimpleButton(text_4, alarmWindow.Width / 3, alarmWindow.Height / 4);
            SimpleButton button_5 = new SimpleButton(text_5, alarmWindow.Width / 3, alarmWindow.Height / 4);
            SimpleButton button_6 = new SimpleButton(text_6, alarmWindow.Width / 3, alarmWindow.Height / 4);
            SimpleButton button_7 = new SimpleButton(text_7, alarmWindow.Width / 3, alarmWindow.Height / 4);
            SimpleButton button_8 = new SimpleButton(text_8, alarmWindow.Width / 3, alarmWindow.Height / 4);
            SimpleButton button_9 = new SimpleButton(text_9, alarmWindow.Width / 3, alarmWindow.Height / 4);
            SimpleButton button_erase = new SimpleButton(text_del, alarmWindow.Width / 3, alarmWindow.Height / 4);

            button_0.BackColor = Colors.White;
            button_1.BackColor = Colors.White;
            button_2.BackColor = Colors.White;
            button_3.BackColor = Colors.White;
            button_4.BackColor = Colors.White;
            button_5.BackColor = Colors.White;
            button_6.BackColor = Colors.White;
            button_7.BackColor = Colors.White;
            button_8.BackColor = Colors.White;
            button_9.BackColor = Colors.White;
            button_erase.BackColor = Colors.White;

            textCode = new Text(Resources.GetFont(Resources.FontResources.NinaB), "");
            textCode.HorizontalAlignment = HorizontalAlignment.Center;
            textCode.VerticalAlignment = VerticalAlignment.Center;
            Panel pan_text = new Panel();
            pan_text.Children.Add(textCode);
            pan_text.Width = alarmWindow.Width / 3;
            pan_text.Height = alarmWindow.Height / 4;
            pan_text.HorizontalAlignment = HorizontalAlignment.Center;

            Canvas.SetTop(button_7, 0);
            Canvas.SetLeft(button_7, 0);
            canvas.Children.Add(button_7);

            Canvas.SetTop(button_8, 0);
            Canvas.SetLeft(button_8, alarmWindow.Width / 3);
            canvas.Children.Add(button_8);

            Canvas.SetTop(button_9, 0);
            Canvas.SetLeft(button_9, 2 * alarmWindow.Width/3);
            canvas.Children.Add(button_9);

            Canvas.SetTop(button_4, alarmWindow.Height / 4);
            Canvas.SetLeft(button_4, 0);
            canvas.Children.Add(button_4);

            Canvas.SetTop(button_5, alarmWindow.Height / 4);
            Canvas.SetLeft(button_5, alarmWindow.Width / 3);
            canvas.Children.Add(button_5);

            Canvas.SetTop(button_6, alarmWindow.Height / 4);
            Canvas.SetLeft(button_6, 2 * alarmWindow.Width / 3);
            canvas.Children.Add(button_6);

            Canvas.SetTop(button_1, alarmWindow.Height / 2);
            Canvas.SetLeft(button_1, 0);
            canvas.Children.Add(button_1);

            Canvas.SetTop(button_2, alarmWindow.Height / 2);
            Canvas.SetLeft(button_2, alarmWindow.Width / 3);
            canvas.Children.Add(button_2);

            Canvas.SetTop(button_3, alarmWindow.Height / 2);
            Canvas.SetLeft(button_3, 2 * alarmWindow.Width / 3);
            canvas.Children.Add(button_3);

            Canvas.SetBottom(button_0, 0);
            Canvas.SetLeft(button_0, 0);
            canvas.Children.Add(button_0);

            Canvas.SetBottom(button_erase, 0);
            Canvas.SetLeft(button_erase, alarmWindow.Width/3);
            canvas.Children.Add(button_erase);

            Canvas.SetBottom(pan_text, 0);
            Canvas.SetLeft(pan_text, 2 * alarmWindow.Width / 3);
            canvas.Children.Add(pan_text);

            button_0.Click += new EventHandler(Button0_Click);
            button_1.Click += new EventHandler(Button1_Click);
            button_2.Click += new EventHandler(Button2_Click);
            button_3.Click += new EventHandler(Button3_Click);
            button_4.Click += new EventHandler(Button4_Click);
            button_5.Click += new EventHandler(Button5_Click);
            button_6.Click += new EventHandler(Button6_Click);
            button_7.Click += new EventHandler(Button7_Click);
            button_8.Click += new EventHandler(Button8_Click);
            button_9.Click += new EventHandler(Button9_Click);
            button_erase.Click += new EventHandler(ButtonErase_Click);

            alarmWindow.Child = canvas;
            alarmWindow.Visibility = Visibility.Visible;
        }

        private void SwitchOffAlarm()
        {
            if (code == "0000")
            {
                alarmWindow.Close();
                Alarm.getInstance().switchOffAlarm();
            }
            else
            {
                code = "";
                textCode.TextContent = "";
            }

        }

        private void Button0_Click(object sender, EventArgs evt)
        {
            code += "0";
            if (code.Length == 4)
                SwitchOffAlarm();
            else
                textCode.TextContent = textCode.TextContent + "*";
        }

        private void Button1_Click(object sender, EventArgs evt)
        {
            code += "1";
            if (code.Length == 4)
                SwitchOffAlarm();
            else
                textCode.TextContent = textCode.TextContent + "*";
        }

        private void Button2_Click(object sender, EventArgs evt)
        {
            code += "2";
            if (code.Length == 4)
                SwitchOffAlarm();
            else
                textCode.TextContent = textCode.TextContent + "*";
        }

        private void Button3_Click(object sender, EventArgs evt)
        {
            code += "3";
            if (code.Length == 4)
                SwitchOffAlarm();
            else
                textCode.TextContent = textCode.TextContent + "*";
        }

        private void Button4_Click(object sender, EventArgs evt)
        {
            code += "4";
            if (code.Length == 4)
                SwitchOffAlarm();
            else
                textCode.TextContent = textCode.TextContent + "*";
        }

        private void Button5_Click(object sender, EventArgs evt)
        {
            code += "5";
            if (code.Length == 4)
                SwitchOffAlarm();
            else
                textCode.TextContent = textCode.TextContent + "*";
        }

        private void Button6_Click(object sender, EventArgs evt)
        {
            code += "6";
            if (code.Length == 4)
                SwitchOffAlarm();
            else
                textCode.TextContent = textCode.TextContent + "*";
        }

        private void Button7_Click(object sender, EventArgs evt)
        {
            code += "7";
            if (code.Length == 4)
                SwitchOffAlarm();
            else
                textCode.TextContent = textCode.TextContent + "*";
        }

        private void Button8_Click(object sender, EventArgs evt)
        {
            code += "8";
            if (code.Length == 4)
                SwitchOffAlarm();
            else
                textCode.TextContent = textCode.TextContent + "*";
        }

        private void Button9_Click(object sender, EventArgs evt)
        {
            code += "9";
            if (code.Length == 4)
                SwitchOffAlarm();
            else
                textCode.TextContent = textCode.TextContent + "*";
        }

        private void ButtonErase_Click(object sender, EventArgs evt)
        {
            if (code.Length != 0) 
            {
                code = code.Substring(0, code.Length - 1);
                textCode.TextContent = textCode.TextContent.Substring(0, code.Length);
            }
        }
    }
}
